// pages/user/registration_selection/registration_selection.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    rollbackPage: null
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      rollbackPage: decodeURIComponent(options.rollback)
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  gotoAgreementPage: function() {
    wx.navigateTo({
      url: "/pages/user/setting/agreement/agreement",
    })
  },
 
  goBackHome: function (e) {
    wx.switchTab({
      url: "/pages/index/index/index",
    })
  },
  getPhoneNumber: function (e) {
    var _this = this;
    console.log(e.detail.errMsg);
    console.log(e.detail.iv);
    console.log(e.detail.encryptedData);
    wx.request({
      url: app.globalData.apiDomain + 'crowdsource/wechat/regUser',
      header: { 'Content-Type': 'application/json' },
      method: 'POST',
      data: {
        'openId': app.globalData.openId,
        'sessionKey': app.globalData.sessionKey,
        'encryptedData': e.detail.encryptedData,
        'iv': e.detail.iv
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if (res.data.object.weChatUser && res.data.object.weChatUser.userToken) {
            console.log(res.data.object.weChatUser);
            app.globalData.token = res.data.object.weChatUser.userToken;
            app.globalData.userInformation = res.data.object.weChatUser;

            wx.showToast({
              title: '注册成功',
              success: function () {
                if (_this.data.rollbackPage.indexOf('?') != -1) {
                  wx.redirectTo({
                    url: _this.data.rollbackPage
                  })
                } else {
                  wx.reLaunch({
                    url: _this.data.rollbackPage
                  })
                }
              }
            });
          } else {

          }
        }
      }
    });
  }
})